﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ApprovalController.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the ApprovalController type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace UnityWeb.Controllers
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web.Mvc;

  using Entities;

  using Exceptions;
  using Logging;
  using ServiceInterfaces;
  using UnityWeb.Filters;
  using UnityWeb.Models.Approval;
  using UnityWeb.Models.Shared;

  [AuthorizeLoggedInUser]
  public class ApprovalController : BaseController
  {
    private readonly IApprovalService approvalService;
    private readonly IDocumentService documentService;
    private readonly ICartItemService cartItemService;

    public ApprovalController(IApprovalService approvalService, IDocumentService documentService, ICartItemService cartItemService, ILogger logger)
      : base(logger)
    {
      this.approvalService = approvalService;
      this.documentService = documentService;
      this.cartItemService = cartItemService;
    }

    [HttpPost]
    public ActionResult Document(ApproveDocumentsViewModel approveDocumentsViewModel)
    {
      var documentsAlreadyApproved = new List<String>();
      var documentsApproved = new List<String>();
      var documentsAlreadyRejcted = new List<String>();

      foreach (var document in approveDocumentsViewModel.ApproveDocumentViewModel.Where(d => d.Selected))
      {
        this.ApproveDocument(document.DocumentId, document.ManCo, document.DocType, document.SubDocType, documentsAlreadyApproved, documentsApproved, documentsAlreadyRejcted);
      }

      var documentWarningsViewModel = new DocumentWarningsViewModel
                                        {
                                          DocumentsAlreadyApproved = string.Join(",", documentsAlreadyApproved.ToArray()),
                                          DocumentsApproved = documentsApproved.Count,
                                          DocumentsAlreadyRejected = string.Join(",", documentsAlreadyRejcted.ToArray())
                                        };

      return this.PartialView("_DocumentWarnings", documentWarningsViewModel);
    }

    [HttpPost]
    public ActionResult Grid(String grid)
    {
      BatchApprovalResults approvalResults = this.approvalService.ApproveGrid(this.HttpContext.User.Identity.Name, grid);

      var documentWarningsViewModel = new DocumentWarningsViewModel
                                        {
                                          DocumentsApproved = approvalResults.ApprovedDocuments.Count,
                                          DocumentsAlreadyApproved = String.Join(",", approvalResults.AlreadyApprovedDocuments),
                                          DocumentsAlreadyRejected = String.Join(",",  approvalResults.AlreadyRejectedDocuments)
                                        };

      return this.PartialView("_DocumentWarnings", documentWarningsViewModel);
    }

    [HttpPost]
    public ActionResult Basket(ApproveDocumentsViewModel approveDocumentsViewModel)
    {
      String userName = this.HttpContext.User.Identity.Name;
      String cartId = CartItem.GetCartId(this.HttpContext);

      BatchApprovalResults approvalResults = this.approvalService.ApproveBasket(userName, cartId);

      var documentWarningsViewModel = new DocumentWarningsViewModel
      {
        DocumentsApproved = approvalResults.ApprovedDocuments.Count,
        DocumentsAlreadyApproved = String.Join(",", approvalResults.AlreadyApprovedDocuments),
        DocumentsAlreadyRejected = String.Join(",", approvalResults.AlreadyRejectedDocuments)
      };

      return this.PartialView("_DocumentWarnings", documentWarningsViewModel);
    }

    private void ApproveDocument(String documentId, String manCo, String docType, String subDocType, List<String> documentsAlreadyApproved, List<String> documentsApproved, List<String> documentsAlreadyRejcted)
    {
      try
      {
        this.approvalService.ApproveDocument(this.HttpContext.User.Identity.Name, documentId, manCo, docType, subDocType);

        documentsApproved.Add(documentId);
      }
      catch (DocumentAlreadyApprovedException)
      {
        documentsAlreadyApproved.Add(documentId);
      }
      catch (DocumentAlreadyRejectedException)
      {
        documentsAlreadyRejcted.Add(documentId);
      }
    }
  }
}
